package com.cxd.base.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author wtj
 * @date 2016年2月19日 下午5:56:39
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
public class SerializeUtil {
	
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {

		}
		return null;
	}

	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {

		}
		return null;
	}
	
	
	public static void main(String[] arg){
		List li = new ArrayList();
		li.add("cxd");
		byte[] by = SerializeUtil.serialize(li);
		
		List l = (List)SerializeUtil.unserialize(by);
		for(Object s:l){
			System.out.println(s);
		}
	}
}
